package com.rpframework.website.device.act.api;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.rpframework.core.utils.TagUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.act.admin.AdminAct;
import com.rpframework.website.device.domain.CProperty;
import com.rpframework.website.device.domain.Information;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.exception.APICodeException;
import com.rpframework.website.device.service.CPropertyService;
import com.rpframework.website.device.service.InformationService;
import com.rpframework.website.device.service.PraiseService;
@Controller
@RequestMapping("api/information")
public class ApiInformationAct extends AdminAct{
	Gson gson =new Gson();
	@Resource InformationService informationService;
	 @Resource CPropertyService cpropertyService;
	 @Resource PraiseService praiseService;
	
	
	//跳蚤信息
	 /**
	  * 热门资讯
	  * @param pager
	  * @param model
	  * @param callback
	  * @param session
	  * @return
	  * @time 2016年3月8日 下午9:00:32
	  */
		@RequestMapping(value="/list" ,produces = "application/json; charset=utf-8")
		public @ResponseBody String listwineds(@RequestParam(value="pager", required=false)Pager<Information> pager, Map<Object, Object> model,
				@RequestParam(value = "callback",required = false ) String callback,
				HttpSession session){
			if(pager==null){
				pager=new Pager<Information>();
			}
			User user = getSessionUser(session);
			if(user == null){
				throw new APICodeException(-4, "你还没登陆!");
			}	
			pager.getSearchMap().put("category", user.getEstateId()+"");
			pager=informationService.getpager(pager);
			List<Information> list=pager.getItemList();
			JsonObject json=new JsonObject();
			json.addProperty("totalPages", pager.getTotalPages());
			json.addProperty("currentPage", pager.getCurrentPage());
			json.addProperty("totalCount", pager.getTotalCount());
			JsonArray array=new JsonArray();
			json.add("arrays", array);
			for(Information act: list){
		
				JsonObject jsonObj = gson.toJsonTree(act).getAsJsonObject();
				jsonObj.addProperty("starttime", TagUtils.formatDate(act.getStarttime()));
				array.add(jsonObj);
			}
			if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
			return ""+json ;
		}
		
		//查询单个
		
		@RequestMapping(value="/findByid",produces = "application/json; charset=utf-8")
		public @ResponseBody String listByid(
				@RequestParam String id,
				@RequestParam(value = "callback",required = false ) String callback,
				HttpSession session){
			User user = getSessionUser(session);
			if(user == null){
				throw new APICodeException(-4, "你还没登陆!");
			}	
			Information  flea=informationService.selectOnlyOne(Integer.parseInt(id));
			
			CProperty cc=cpropertyService.selectcProperty(flea.getPropertyid()+"");

			JsonObject json=new JsonObject();
			json.addProperty("propertyname", cc.getCommunityname());
			json.addProperty("propertpic", cc.getPic());
			json.addProperty("msg", flea.getMsg());
			json.addProperty("msgpic", flea.getMsgpic());
			json.addProperty("onepic", flea.getOnepic());
			json.addProperty("title", flea.getTitle());
			json.addProperty("dianzan", flea.getDianzan());
			json.addProperty("commentsnum", flea.getCommentsnum());
			json.addProperty("starttime", TagUtils.formatDate(flea.getStarttime()));
			json.addProperty("favoriteCount",praiseService.priceCount(Integer.valueOf(id), 1,2));//评论表里type 查点赞总数  status=2 为收藏  status=3为点赞
			json.addProperty("isFavorite",praiseService.isPraiseByUser(Integer.valueOf(id), 1, user.getId()));// 1可以收藏  2以收藏
			json.addProperty("praiseCount",praiseService.priceCount(Integer.valueOf(id), 1,3));
			json.addProperty("isPraise",praiseService.isPraiseByUser(Integer.valueOf(id), 1, user.getId()));
			if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
			return ""+json ;
		}

}
